<!--华视读卡器-->
<template>
    <div class='readCardjy'>
        <object ref="CertCtl" type="application/cert-reader" width="0" height="0">
           <p style="color:#FF0000;">请使用360浏览器。</p>
        </object>  
      <el-button type="primary" @click="btnClicks">读取</el-button>
     
  </div>
</template>
<script>
export default {
  props:['id'],
  name:'read-cardhs',
  data(){
      return{
         
      }
  },
  created(){
     
  },
  methods:{
      btnClicks(){
        let that=this;
        var CertCtl =this.$refs.CertCtl;	
        var results = CertCtl.connect();
        var obj = eval('(' + results + ')');	
        if(obj.resultFlag==0){
        try {
            var startDt = new Date();
            var result = CertCtl.readCert();
            var endDt = new Date();
            var resultObj = eval('('+result+')');

            let begin_data="data:image/jpeg;base64,";
            let datas={};

            
                if (resultObj.resultFlag == 0) {
                    
                    datas.labelA = resultObj.resultContent.partyName;
                    if(resultObj.resultContent.gender==1){
                        datas.Sex ="男";
                    }else if(resultObj.resultContent.gender==0){
                        datas.Sex= "女";
                    }			
                    datas.mz = resultObj.resultContent.nation+"族";
                    datas.Born=  resultObj.resultContent.bornDay.substring(0,4)+"-"+resultObj.resultContent.bornDay.substring(4,6)+"-"+resultObj.resultContent.bornDay.substring(6,8);
                    datas.Address = resultObj.resultContent.certAddress;
                    datas.CardNo = resultObj.resultContent.certNumber;	
                    datas.image=begin_data+resultObj.resultContent.identityPic;	
                    
                    var date = new Date();
                    let curYear=date .getFullYear(); //获取完整的年份(4位)
                    let age=Number(curYear)-Number(datas.Born.substring(0,4));
                    age=age.toFixed(0);
                    datas.age=age;
                    let ids=that.id.split("|");
                    
                    for(let i=0;i<ids.length;i++){
                        let id_=ids[i].split(":");
                        console.log("ids==="+ids[i])
                        if(id_[0]!="image"){
                            let data={};
                            let val=datas[id_[0]];
                            data.id=id_[1];
                            data.val=val;
                            
                            that.$emit("input",JSON.stringify(data));
                        }else  if(id_[0]=="image"){
                            that.$post("business","/api/file/fileUploadBase64",{data:resultObj.resultContent.identityPic,model:"头像",type:"头像",name:"头像"}).then(data=>{
                            
                                let url=data[0].url;
                                console.log("uploadImg==========================");
                                console.log(url);
                                let data_={};
                                data_.id=id_[1];
                                data_.val=url;
                                that.$emit("input",JSON.stringify(data_));
                            });	
                            /*let data={};
                            data.id=id_[1];
                            data.val=message;
                            sef.$emit("input",JSON.stringify(data));*/
                        }
                        
                    }
                }
            
            
        } catch(e) {
            alert(e);
        }
        }else if(obj.resultFlag==-1){
            alert("连接失败！");
        }
      },
      
     

          
      
      
  }
}
</script>